﻿


using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using LaserMark.Models;
using LaserMark.Components.Utils;

namespace LaserMark.Components.Components;

public partial class TasksView
{
    [Inject]
    [NotNull]
    private ErpHttpClient? ErpHttpClient { get; set; }

    [Inject]
    [NotNull]
    private DialogService? DialogService { get; set; }

    private List<Opts> Opts = new();

    private async Task<QueryData<Opts>> OnQueryAsync(QueryPageOptions option)
    {
        try
        {
            var mes = await ErpHttpClient.GetFromJsonAsync<List<Opts>>("/sales/optimaze/eventualcontents");
            if (mes != null)
            {
                foreach (var opts in mes!.Data)
                {
                    if (opts.OptProduceState)
                    {
                        Opts.Add(opts);
                    }
                }

                return new QueryData<Opts>() { Items = mes.Data };
            }
            else
            {
                return new QueryData<Opts>();
            }
        }
        catch (Exception e)
        {
            return new QueryData<Opts>();
        }
    }

    private async Task OnListViewItemClick(Opts opt)
    {
        try
        {
            var mes = await ErpHttpClient.GetFromJsonAsync<string>($"/sales/optimaze/eventualNames/{opt.OptId}");

            await DialogService.Show(new DialogOption()
            {
                Title = "opt",
                BodyTemplate = BootstrapDynamicComponent
                    .CreateComponent<CodeEditor>(new Dictionary<string, object?>()
                    {
                        [nameof(CodeEditor.Language)] = "ini",
                        [nameof(CodeEditor.ShowLineNo)] = true,
                        [nameof(CodeEditor.Value)] = mes!.Msg,
                        [nameof(CodeEditor.Theme)] = "vs-dark"
                    }).Render()
            });
        }
        catch (Exception ex)
        {
        }
    }

    private Task OnValueChanged(bool arg, Opts opts)
    {
        // if (!Opts.Any(s => s.OptId == opts.OptId))
        // {
        //     Opts.Add(opts);
        // }
        return Task.CompletedTask;
    }
}
